package com.erp.programmer.controller;


import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dto.ClientCompanyDto;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.service.IClientCompanyService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Takagi3
 * @QQ: 870089046
 * @VX: HandsomeMY_
 * @Date: create in 2021/5/23 20:08
 */

@RestController
@CrossOrigin
@RequestMapping("/client_company")
public class ClientCompanyController {

    @Autowired
    private IClientCompanyService iClientCompanyService;


    @PostMapping("/add")
    public ResponseDto<List<ClientCompanyDto>> add(@RequestBody ClientCompanyDto clientCompanyDto){


        // 1.字段验证
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(clientCompanyDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }

        return iClientCompanyService.add(clientCompanyDto);

    }



    @PostMapping("/edit")
    public ResponseDto<List<ClientCompanyDto>> edit(@RequestBody ClientCompanyDto clientCompanyDto){
        // 1.字段验证
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(clientCompanyDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }

        return iClientCompanyService.edit(clientCompanyDto);
    }

    @PostMapping("/search")
    public ResponseDto<List<ClientCompanyDto>> search(@RequestBody ClientCompanyDto clientCompanyDto){


        return iClientCompanyService.search(clientCompanyDto);
    }
    
    /**
     * 分页
     * 搜索获取全部客户公司信息
     */
    @PostMapping("/get_data")
    public ResponseDto<PageDto<ClientCompanyDto>> getAll(@RequestBody PageDto<ClientCompanyDto> pageDto) {

        return iClientCompanyService.getAll(pageDto);
    }
    /**
     * 分页
     *保存、修改客户公司信息
     */
    @PostMapping("/save")
    public ResponseDto<PageDto<ClientCompanyDto>> saveClientCompany(@RequestBody ClientCompanyDto clientCompanyDto) {
        //非空判断
        if (clientCompanyDto == null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(clientCompanyDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }

        return iClientCompanyService.saveClientCompany(clientCompanyDto);

    }


    /**
     * 分页
     *删除客户公司信息
     */
    @PostMapping("/delete")
    public ResponseDto<PageDto<ClientCompanyDto>> deleteClientCompany(@RequestBody ClientCompanyDto clientCompanyDto) {

        if (clientCompanyDto == null || CommonUtil.isEmpty(clientCompanyDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        return iClientCompanyService.deleteClientCompany(clientCompanyDto);

    }
}
